﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities.Helper
{
    public class Logger
    {
        private static bool LogToFile = true;

        public Logger() { }

        public Logger(Exception logMsg)
        {
            Log(logMsg.ToString());
        }

        public Logger(string logMsg)
        {
            Log(logMsg);
        }

        public static void Log(Exception logMsg)
        {
            Log(logMsg.ToString());
        }

        public static void Log(string logMsg)
        {

            if (LogToFile)
            {
                string strPath = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(Declarations)).CodeBase);
                strPath = strPath.Replace("file:\\", "");

                if (!strPath.EndsWith("\\"))
                    strPath = strPath + "\\";

                if (strPath.Contains("bin\\"))
                {
                    strPath = strPath.Replace("\\bin\\", "\\");

                }

                string strLogPath = "logs";
                if (!string.IsNullOrEmpty(strLogPath))
                {
                    //strLogPath = Path.Combine(strLogPath, "");
                    if (!System.IO.Directory.Exists(strLogPath))
                    {
                        System.IO.Directory.CreateDirectory(strLogPath);
                    }
                    string strLogFile = strLogPath + "\\" + "_" + DateTime.Now.ToString("yyMMdd") + ".log";
                    using (StreamWriter objSW = new StreamWriter(strLogFile, true))
                    {
                        //objSW.WriteLine(DateTime.Now.ToString() + " - " + Location + Environment.NewLine + Message + Environment.NewLine + Environment.NewLine);
                        objSW.WriteLine(DateTime.Now.ToString() + " - " + logMsg + Environment.NewLine + Environment.NewLine);
                    }
                }
            }
            else
            {
                Console.Write(logMsg);
            }
        }
    }
}
